package com.yhp.controller;

import com.google.gson.Gson;
import com.yhp.bean.*;
import com.yhp.mvc.ResponseBody;
import com.yhp.service.impl.CourierService;
import com.yhp.service.impl.ExpressService;
import com.yhp.util.DateFormatUtil;
import com.yhp.util.GsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


public class CourierController {

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Courier> all = CourierService.findAll(true, offset, pageNumber);
        List<BootStrapCourier> list = new ArrayList<>();
        for (Courier e : all) {
            String numberId = e.getNumberId() == null ? "" : "**************";
            String inTime = DateFormatUtil.format(e.getInTime());
            String time =e.getTime()==null?null:DateFormatUtil.format(e.getTime());
            BootStrapCourier bootStrapCourier = new BootStrapCourier(e.getId(), e.getName(), e.getPhone(), numberId, e.getPassWord(), e.getNumberAll(), inTime, time);
            list.add(bootStrapCourier);

        }
        int total = CourierService.total();
        ResultData<BootStrapCourier> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(total);
        return GsonUtil.toJSON(data);

    }

    @ResponseBody("/courier/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        System.out.println(name);
        String phone = request.getParameter("phone");
        String numberId = request.getParameter("numberId");
        String passWord = request.getParameter("passWord");
        Courier courier = new Courier();
        courier.setName(name);
        courier.setPhone(phone);
        courier.setNumberId(numberId);
        courier.setPassWord(passWord);
        boolean insert = CourierService.insert(courier);
        Message msg = new Message();
        if(insert){
            msg.setStatus(0);
            msg.setResult("添加成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("添加失败");
        }
        String s = GsonUtil.toJSON(msg);
        return s;
    }


    @ResponseBody("/courier/findPhone.do")
    public String findByPhone(HttpServletRequest request, HttpServletResponse response){
        String findPhone = request.getParameter("findPhone");
        Courier byPhone = CourierService.findByPhone(findPhone);
        Message msg = new Message();
        if(byPhone != null){
            msg.setStatus(0);
            msg.setResult("查找成功");
            msg.setData(byPhone);
        }else {
            msg.setStatus(-1);
            msg.setResult("查找失败");
        }
        String s = GsonUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String numberId = request.getParameter("numberId");
        String passWord = request.getParameter("passWord");
        Courier courier = new Courier();
        courier.setName(name);
        courier.setPhone(phone);
        courier.setNumberId(numberId);
        courier.setPassWord(passWord);
        boolean update = CourierService.update(Integer.parseInt(id), courier);
        Message msg = new Message();
        if(update){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String s = GsonUtil.toJSON(msg);

        return s;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        int cid = Integer.parseInt(id);
        boolean delete = CourierService.delete(cid);
        Message msg = new Message();
        if(delete){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }

        String s = GsonUtil.toJSON(msg);
        return s;
    }
}
